package com.hybao.topApi;

import java.util.Calendar;
import java.util.Date;
import java.util.List;

import com.hybao.common.TopBaseClient;
import com.taobao.api.ApiException;
import com.taobao.api.domain.Trade;
import com.taobao.api.request.TradesSoldGetRequest;
import com.taobao.api.response.TradesSoldGetResponse;

public class TradeTopService extends TopBaseClient{
	
	public List<Trade> getTradesFromTopWithNick(String nick, String sessionKey,Date lastSyncDate){
		TradesSoldGetRequest req=new TradesSoldGetRequest();
		String fields = "seller_nick, buyer_nick, title, type, created, tid, seller_rate, buyer_rate, status, ";
		fields = fields + "payment, discount_fee, adjust_fee, post_fee, total_fee, pay_time, end_time, modified,";
		fields = fields + "consign_time, buyer_obtain_point_fee, point_fee, real_point_fee, received_payment,";
		fields = fields + "commission_fee, pic_path, num_iid, num, price, cod_fee, cod_status, shipping_type, receiver_name,";
		fields = fields + "receiver_state, receiver_city, receiver_district, receiver_address, receiver_zip, receiver_mobile, receiver_phone";
		req.setFields(fields);
		Calendar calendar = Calendar.getInstance();
		req.setEndCreated(calendar.getTime());
		if(lastSyncDate != null){
			req.setStartCreated(lastSyncDate);
		}
		else{
			calendar.add(Calendar.DATE,-30);
			req.setStartCreated(calendar.getTime());
		}
		TradesSoldGetResponse response = null;
		try {
			response = this.getClient().execute(req , sessionKey);
		} catch (ApiException e) {
			this.getLog().error("trade top get error",e);
		}
		if(response != null && response.isSuccess()){
			return response.getTrades();
		}
		else{
			return null;
		}
	}
}
